package com.yowasa.xiuxian.engine.message.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.item.UserItemBo;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.QualityEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.List;


@CMD(name = "/强化", scope = CmdScopeEnum.ALL)
public class StrengthenHandler extends CmdMessageHandler {
    public static final Logger logger = LoggerFactory.getLogger(StrengthenHandler.class);

    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private ItemHelper itemHelper;


    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUser(message);
        List<String> params = getAllParam(message);

        if (params.size() < 2) {
            messageHelper.replyMys(message, "指令有误，正确指令为 /强化 装备名 品质, 仅可强化背包中的装备\n\n装备强化需要材料为\n 萃凝晶 * 20\n无色晶体 * 4 * 对应品质\n品质对应矿石 * 20\n矿石品质顺序：\n\"玄铁矿\", \"白铁矿\", \"水晶矿\", \"魔晶矿\", \"紫晶矿\"");
            return;
        }
        String itemName = params.get(0);
        QualityEnum qualityEnum = QualityEnum.getByName(params.get(1));
        if (ObjectUtil.isNull(qualityEnum)) {
            qualityEnum = QualityEnum.getById(1);
        }
        if(qualityEnum.getId() == 6){
            throw new HandlerException("当前装备品质已经是最高了，无法再提升了");
        }
        UserItemBo myEqip = itemHelper.getMyEqip(user.getId(), itemName, qualityEnum.getId());
        if(ObjectUtil.isNull(myEqip)){
            throw new HandlerException("背包中没有这种装备 : "+itemName);
        }

        if(itemHelper.checkStrength(user,myEqip)){
            //材料足够，开始升级
            int i = RandomUtil.randomInt(7);
            logger.info("随机数为：{}  ， 品质为 ： {}",i,qualityEnum.getId());
            if(user.getUserId().equals("299331411")){
                logger.info("QWQ----------------");
                i+=3;
            }
            if(i - qualityEnum.getId() > 0){
                //升级成功了
                itemHelper.dropItem(myEqip,1);
                ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myEqip.getItemName());
                itemHelper.addEquip(user,itemInterface,1,qualityEnum.getId()+1);
                messageHelper.replyMys(message, MessageFormat.format("强化成功，你获得了[{0}]{1} * 1",QualityEnum.getById(qualityEnum.getId()+1).getName(),myEqip.getItemName()));
            }else {
                StringBuilder sb = new StringBuilder();
                sb.append("强化失败了T~T");
                //升级失败
                if(qualityEnum.getId() > 3 && Math.random() < 0.15 * qualityEnum.getId()){
                    // 3 4 5 升级失败，概率触发降级
                    itemHelper.dropItem(myEqip,1);
                    ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myEqip.getItemName());
                    itemHelper.addEquip(user,itemInterface,1,qualityEnum.getId()-1);
                    sb.append("\n 因为强化失败，装备品质下降一级");
                }
                messageHelper.replyMys(message,sb.toString());
            }
        }
    }


    public String desc() {
        return new StringBuffer().append("背包道具相关指令，未穿戴的装备也会存在这里\n")
                .append("当同名称不同品质的装备同时存在背包时，请使用装备相关指令进行穿戴\n")
                .append("当不存在同名装备时，可使用 /道具 使用 进行快捷穿戴")
                .toString();

    }
}
